//
//  GHSNotSaleViewController.m
//  penzer
//
//  Created by 还带大道 on 2018/11/20.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "GHSNotSaleViewController.h"
#import "GHSStockTableViewCell.h"
#import "GHSGoodsModel.h"
#import "GHSStockManagerAlertView.h"

@interface GHSNotSaleViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,weak) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray<GHSGoodsModel *> *items;
@property (nonatomic,strong) GHSStockManagerAlertView *managerAlertV;

@end

@implementation GHSNotSaleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTableView];
    for (NSInteger index = 0; index < 5; index++) {
        GHSGoodsModel *model = [GHSGoodsModel new];
        [model addObserver:self forKeyPath:@"selected" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
        [self.items addObject:model];
    }
}


- (void)dealloc
{
    [self.items enumerateObjectsUsingBlock:^(GHSGoodsModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeObserver:self forKeyPath:@"selected"];
    }];
}



- (void)setupTableView
{
    UITableView *tableView = [UITableView new];
    _tableView =tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:tableView];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GHSStockTableViewCell class]) bundle:nil]  forCellReuseIdentifier:NSStringFromClass([GHSStockTableViewCell class])];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.items.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 140;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GHSStockTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GHSStockTableViewCell class])];
    cell.model = self.items[indexPath.row];
    return cell;
}



- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if([keyPath isEqualToString:@"selected"]){
        [self statistics];
    }else{
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}



#pragma mark--统计选中数量
- (void)statistics
{
    __block NSInteger count = 0;
    [self.items enumerateObjectsUsingBlock:^(GHSGoodsModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if(obj.isSelected){
            count++;
        }
    }];
    !self.selectBlock?:self.selectBlock(self.items.count,count);
}


#pragma mark--删除
- (void)deleteGoodsComplete:(void (^)(BOOL))complete
{
    !complete?:complete(YES);
}

#pragma mark--上架
- (void)putOnShelfGoodsComplete:(void (^)(BOOL))complete
{
    self.managerAlertV.hidden = NO;
    __weak typeof(_managerAlertV) weakM = _managerAlertV;
    _managerAlertV.onClickBtn1Block = ^{
        weakM.hidden = YES;
        !complete?:complete(YES);
    };
}



#pragma mark--getter&&setter

- (NSMutableArray<GHSGoodsModel *> *)items
{
    if(!_items){
        _items = [NSMutableArray array];
    }
    return _items;
}


- (void)setEdit:(BOOL)edit
{
    _edit = edit;
    [self.items enumerateObjectsUsingBlock:^(GHSGoodsModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.edit = edit;
    }];
    [self.tableView reloadData];
}


- (void)setSelectAll:(BOOL)selectAll
{
    _selectAll = selectAll;
    [self.items enumerateObjectsUsingBlock:^(GHSGoodsModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.selected = selectAll;
    }];
    [self.tableView reloadData];
}



- (GHSStockManagerAlertView *)managerAlertV
{
    if(!_managerAlertV){
        _managerAlertV = [GHSStockManagerAlertView shareView];
        _managerAlertV.contentV_H.constant = 270;
        [_managerAlertV.btn1 setTitle:@"确认上架" forState:UIControlStateNormal];
        _managerAlertV.btn2.hidden = YES;
        _managerAlertV.frame = RZ_KEYWINDOW.bounds;
        [RZ_KEYWINDOW addSubview:_managerAlertV];
    }
    return _managerAlertV;
}

@end
